#include "Date.h"

Date::Date(int year, int month, int day)
	:_year(year),
	_month(month),
	_day(day)
{}

Date::~Date()
{}

bool Date::IsLeapYear(int year)
{
	return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int Date::GetMonthDays(int year, int month)
{
	static int Month[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (month == 2 && IsLeapYear(year))
		return 29;
	else
		return Month[month];
}

bool Date::operator!=(const Date& d) const
{
	return _year != d._year || _month != d._month || _day != d._day;
}
bool Date::operator==(const Date& d) const
{
	return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator>(const Date& d) const
{
	return (_year > d._year)
		|| (_year == d._year && _month > d._month) 
		|| (_year == d._year && _month == d._month && _day > d._day);
}
bool Date::operator>=(const Date& d) const
{
	return *this == d || *this > d;
}
bool Date::operator<(const Date& d) const
{
	return !(*this >= d);
}
bool Date::operator<=(const Date& d) const
{
	return !(*this > d);
}

Date& Date::operator++()
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}

Date& Date::operator+=(int days)
{
	if (days < 0)
	{
		*this -= -days;
		return *this;
	}
	_day += days;
	while (_day > GetMonthDays(_year, _month))
	{
		_day -= GetMonthDays(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator+(int days)
{
	Date tmp(*this);
	tmp += days;
	return tmp;
}

Date& Date::operator-=(int days)
{
	if(days < 0)
	{
		*this += -days;
		return *this;
	}
	_day -= days;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day += GetMonthDays(_year, _month);
	}
	return *this;
}
Date Date::operator-(int days)
{
	Date tmp(*this);
	tmp -= days;
	return tmp;
}
int Date::operator-(const Date& d)
{
	Date maxDate = *this, minDate = d;
	int flag = 1;
	if (maxDate < minDate)
	{
		maxDate.swap(minDate);
		flag = -flag;
	}
	
	int count = 0;
	while (minDate != maxDate)
	{
		count++;
		++minDate;
	}
	return flag * count;
}

std::ostream& operator<<(std::ostream& out, const Date& d)
{
	std::cout << d._year << "/" << d._month << "/" << d._day << std::endl;
	return out;
}

std::istream& operator>>(std::istream& in, Date& d)
{
	std::cin >> d._year >> d._month >> d._day;
	return in;
}